package de.uhlesoluschns.opensearch.ui;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import de.uhlesoluschns.opensearch.OpenSearch;
import de.uhlesoluschns.opensearch.R;
import de.uhlesoluschns.opensearch.provider.XmlImporter;

public class SearchProvider extends Activity {
	
	private AsyncTask<Uri, Exception, ContentValues> importerTask = new AsyncTask<Uri, Exception, ContentValues>() {
		@Override
		protected void onPreExecute() {
			setProgressBarIndeterminateVisibility(true);
		}
		
		@Override
		protected void onPostExecute(final ContentValues engine) {
			setProgressBarIndeterminateVisibility(false);
			if(engine != null) {
				((TextView) findViewById(R.id.engineShortName)).setText(engine.getAsString("shortName"));
				((TextView) findViewById(R.id.engineDescription)).setText(engine.getAsString("description"));
				
				byte [] data = engine.getAsByteArray("image");
				if(data != null) {
					((ImageView) findViewById(R.id.engineImage)).setImageBitmap(BitmapFactory.decodeByteArray(data, 0, data.length));
				}
				
				((Button) findViewById(R.id.addEngine)).setOnClickListener(new OnClickListener() {
					public void onClick(View v) {
						ContentResolver resolver = getContentResolver();
						resolver.insert(OpenSearch.Provider.CONTENT_URI, engine);
						Toast.makeText(SearchProvider.this, R.string.ui_import_success, Toast.LENGTH_LONG).show();
						finish();
					}
				});
			}
		}
		
		@Override
		protected void onProgressUpdate(Exception... values) {
			Toast.makeText(SearchProvider.this, values[0].getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
		
		@Override
		protected ContentValues doInBackground(Uri... params) {
			ContentValues engine = null;
			XmlImporter importer = new XmlImporter();
			try {
				engine = importer.parse(params[0]);
				Bitmap image = importer.loadImage(engine.getAsString("image"));
				if(image != null) {
					if(image.getWidth() > 16 || image.getHeight() > 16) {
						image = Bitmap.createScaledBitmap(image, 16, 16, true);
					}
					ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
					image.compress(CompressFormat.PNG, 100, imageStream);
					engine.put("image", imageStream.toByteArray());
				} else {
					engine.remove("image");
				}
				return engine;
			} catch (Exception e) {
				e.printStackTrace();
				publishProgress(e);
				return null;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		
		setTitle(R.string.ui_import_title);
		setContentView(R.layout.search_engine);
		
		importerTask.execute(getIntent().getData());
	}
}
